package com.aaa.inbound.controller;


import com.aaa.entity.inbound.*;
import com.aaa.inbound.mapper.ReceiveMapper;
import com.aaa.inbound.service.ReceiveDetailService;
import com.aaa.inbound.service.ReceiveService;
import com.aaa.inbound.service.WarehousingDetailService;
import com.aaa.inbound.service.WarehousingService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/receive")
@Api(description = "接货单管理")
public class ReceiveController {
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private ReceiveDetailService receiveDetailService;
    @Autowired
    private WarehousingService warehousingService;
    @Autowired
    private WarehousingDetailService warehousingDetail;

    @PostMapping("/searchReceive/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchPurchase(@ApiParam(value = "当前页数") @PathVariable Integer page,
                            @ApiParam(value = "每页个数") @PathVariable Integer limit,
                            @ApiParam(value = "查询条件") @RequestBody Receive receive){
        Page<Receive> receivePage = receiveService.searchReceive(page, limit, receive);
        return R.ok().message("分页查询成功").code(200).data("page",receivePage);
    }

    @PostMapping("/saveReceive/{no}")
    @ApiOperation(value = "根据单号查询")
    public R saveReceive(@ApiParam(value = "id")@PathVariable String no){

        return receiveService.saveReceive(no);
    }

    @PostMapping("/updateState")
    @ApiOperation(value = "修改订单状态")
    public R updateState(@RequestBody Receive receive){
        boolean b = receiveService.saveOrUpdate(receive);
        if (b){
            return R.ok().message("状态修改成功");
        }else {
            return R.error().message("状态修改失败");
        }
    }

    @PostMapping("/delReceive/{id}")
    @ApiOperation(value = "根据ID删除")
    public R delPurchase(@PathVariable Long id){
        Receive receive = receiveService.findById(id);
        if (receive.getState().equals("2")){
            return R.error().code(500).message("订单已经审核，不能删除");
        }else{
            QueryWrapper<ReceiveDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",receive.getId());
            boolean a = receiveDetailService.remove(queryWrapper);
            boolean b = receiveService.removeById(id);
            if (b) {
                return R.ok().message("删除成功").code(200);
            }else {
                return R.error().message("删除失败").code(500);
            }
        }
    }


    @PostMapping("/selectReceiveDetailByNo/{no}")
    @ApiOperation(value = "根据单号查询")
    public R selectReceiveDetailByNo(@PathVariable String no){
        QueryWrapper<Receive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiveno",no);
        Receive one = receiveService.getOne(queryWrapper);

        QueryWrapper<ReceiveDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",one.getId());
        List<ReceiveDetail> list = receiveDetailService.list(wrapper);

        QueryWrapper<Warehousing> wrapperW = new QueryWrapper<>();
        wrapperW.eq("receiveno",no);
        Warehousing warehousing = warehousingService.getOne(wrapperW);

        QueryWrapper<WarehousingDetail> wrapperWD = new QueryWrapper<>();
        wrapperWD.eq("pid",warehousing.getId());
        List<WarehousingDetail> listWD = warehousingDetail.list(wrapperWD);

        List maplist = new ArrayList();
        if (listWD.size()==0){
            for (ReceiveDetail rd : list){
                Map map = new HashMap();
                map.put("pid", warehousing.getId());
                map.put("commodityid", rd.getCommodityid());
                map.put("commoditybar", rd.getCommoditybar());
                map.put("commodityname", rd.getCommodityname());
                map.put("qty", rd.getQty());
                map.put("max", rd.getQty());
                map.put("receiveDetailId", "");
                maplist.add(map);
            }
        }else{
            for (ReceiveDetail rd : list){
                Map map = new HashMap();
                map.put("pid", warehousing.getId());
                map.put("commodityid", rd.getCommodityid());
                map.put("commoditybar", rd.getCommoditybar());
                map.put("commodityname", rd.getCommodityname());
                map.put("qty", rd.getQty());

                boolean pan = true;
                for (WarehousingDetail wd : listWD){
                    if (wd.getQty() == null) {
                        wd.setQty(0);
                    }
                    if (rd.getCommodityid().equals(wd.getCommodityid())) {
                        int max = rd.getQty() - wd.getQty();
                        pan = false;
                        map.put("max", max);
                        map.put("warehousingDetailId", wd.getId());
                    }
                    if (pan) {
                        map.put("max", rd.getQty());
                        map.put("receiveDetailId", "");
                    }
                }
                maplist.add(map);
            }
        }
        return R.ok().code(200).data("maplist",maplist);
    }

}

